import { json } from "@remix-run/node";
import type { ActionFunction } from "@remix-run/node";
import * as shopifyServer from "../services/shopify.server";

export const action: ActionFunction = async ({ request }) => {
  const formData = await request.json();
  const { action: actionType, config, ...params } = formData;

  try {
    switch (actionType) {
      case "getProducts":
        const products = await shopifyServer.getShopifyProducts(config);
        return json({ products });

      case "saveDesign":
        const result = await shopifyServer.saveDesignToShopify(params.designData, config);
        return json(result);

      case "updateProductImage":
        const imageResult = await shopifyServer.updateShopifyProductImage(
          params.productId,
          params.imageId,
          params.imageData,
          config
        );
        return json(imageResult);

      default:
        return json({ error: "Invalid action" }, { status: 400 });
    }
  } catch (error) {
    console.error("Shopify API error:", error);
    return json({ error: "Failed to process Shopify request" }, { status: 500 });
  }
}; 